package ywpt.service.config;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.directwebremoting.annotations.RemoteMethod;
import org.directwebremoting.annotations.RemoteProxy;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;

import ywpt.core.dao.HibernateEntityDao;
import ywpt.model.ClientInfo;
import ywpt.model.ClientKeshi;
import ywpt.model.ClientLinkman;
import ywpt.model.CommonTable;
import ywpt.model.Department;
import ywpt.model.PeopleInfo;
import ywpt.model.ProjectInfo;
import ywpt.web.common.PublicFunction;
@Service("projectInfoService")
@RemoteProxy(name="projectInfoService")
public class ProjectInfoServiceImpl extends HibernateEntityDao<ProjectInfo> implements ProjectInfoService {

	@Override
	public ProjectInfo getProjectInfoById(Long id) {
		return this.get(id);
	}

	@Override
	public int getProjectInfoCountByCriteria(DetachedCriteria query) {
		return this.getCountByCriteria(query);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<ProjectInfo> getProjectInfos(DetachedCriteria query,
			int pageSize, int startIndex) {
		return this.findAllByCriteria(query, pageSize, startIndex);
	}

	@Override
	public void removeProjectInfo(Long id) {
		ProjectInfo projectInfo = this.get(id);
		projectInfo.setDeleteFlag(true);
		this.update(projectInfo);
	}

	@Override
	public void saveProjectInfo(ProjectInfo projectInfo, Long typeId, Long stateId, Long departId, Long peopleId, Long clientId, Long keshiId,Long linkManId,String[] peoId) {
		if(typeId!=null)
			projectInfo.setProjectType(this.get(CommonTable.class, typeId));
		if(stateId!=null)
			projectInfo.setProjectState(this.get(CommonTable.class, stateId));
		if(departId!=null)
			projectInfo.setDepart(this.get(Department.class, departId));
		if(peopleId!=null)
			projectInfo.setResponser(this.get(PeopleInfo.class, peopleId));
		if(clientId!=null)
			projectInfo.setClient(this.get(ClientInfo.class, clientId));
		if(keshiId!=null)
			projectInfo.setKeshi(this.get(ClientKeshi.class, keshiId));
		if(linkManId!=null)
			projectInfo.setLinkman(this.get(ClientLinkman.class, linkManId));
//		if(peoId!=null&&peoId.length>0){
//			String str="";
//			for(int i=0;i<peoId.length;i++){
//				str = str + peoId[i]+",";
//			}
//			projectInfo.setPeople(str);
//		}
		super.save(projectInfo);
	}

	@Override
	public void updateProjectInfo(ProjectInfo projectInfo, Long typeId, Long stateId, Long departId, Long peopleId, Long clientId, Long keshiId,Long linkManId,String[] peoId) {
		if(typeId!=null)
			projectInfo.setProjectType(this.get(CommonTable.class, typeId));
		if(stateId!=null)
			projectInfo.setProjectState(this.get(CommonTable.class, stateId));
		if(departId!=null)
			projectInfo.setDepart(this.get(Department.class, departId));
		if(peopleId!=null)
			projectInfo.setResponser(this.get(PeopleInfo.class, peopleId));
		if(clientId!=null)
			projectInfo.setClient(this.get(ClientInfo.class, clientId));
		if(keshiId!=null)
			projectInfo.setKeshi(this.get(ClientKeshi.class, keshiId));
		if(linkManId!=null)
			projectInfo.setLinkman(this.get(ClientLinkman.class, linkManId));
//		if(peoId!=null&&peoId.length>0){
//			String str="";
//			for(int i=0;i<peoId.length;i++){
//				str = str + peoId[i]+",";
//			}
//			projectInfo.setPeople(str);
//		}else if(projectInfo.getPeople().length()<=0){
//			projectInfo.setPeople(null);
//		}
		super.update(projectInfo);
	}

	
	
	@SuppressWarnings("unchecked")
	@Override
	@RemoteMethod
	public List<ProjectInfo> getProjectInfoByProjectName(String projectName,Long departId, int start,int size) {
		DetachedCriteria query = DetachedCriteria.forClass(ProjectInfo.class);
		query.createAlias("depart", "depart").add(Restrictions.eq("depart.id", departId));
		if ( projectName != null && !projectName.trim().equals("") )
		{
			query.add(Restrictions.or(Restrictions.like("projectName", "%" + projectName + "%"),  
							          Restrictions.like("projectName", "%" + projectName + "%")));
		}
		query.addOrder(Order.asc("id"));
		List<ProjectInfo> projectInfoList = new ArrayList<ProjectInfo>();
		List<ProjectInfo> projectInfoLists = new ArrayList<ProjectInfo>();
		ProjectInfo projectInfo =new ProjectInfo();
		
		projectInfoList.addAll(this.findAllByCriteria(query, size, start));
		int i = 1;
		for ( Iterator<ProjectInfo> itr = projectInfoList.iterator(); itr.hasNext(); i++ ) 
		{
			projectInfo = itr.next();
			String ss = PublicFunction.format(projectInfo.getProjectName() + "",5, "  ", 1); 
			
			projectInfo.setProjectName(ss);
			projectInfoLists.add(projectInfo);
		}
		return projectInfoLists;
	}

	@Override
	@RemoteMethod
	public int getProjectInfoCountByProjeceName(String projectName,Long departId) {
		DetachedCriteria query = DetachedCriteria.forClass(ProjectInfo.class);
		query.createAlias("depart", "depart").add(Restrictions.eq("depart.id", departId));
		if ( projectName != null && !projectName.trim().equals("") )
		{
			query.add(Restrictions.or(Restrictions.like("projectName", "%" + projectName + "%" ),  
							          Restrictions.like("projectName", "%" + projectName + "%" )));
		}
		query.setProjection(null);
		return this.getCountByCriteria(query);
	}	
	
	
	
	
	
	
	
	
	
}
